import { _decorator, AudioSource, CCInteger, CCString, Collider2D, Component, Contact2DType, EventTouch, Graphics, IPhysics2DContact, Node, ParticleSystem2D, Tween, tween, UITransform, v3, Vec3 } from 'cc';
import { EasingType } from 'db://assets/Scripts/Framework/Utils/TweenUtil';
import { MigaManager } from '../MigaManager';
import { Person } from '../Person';
import { WearType } from '../GameData';
const { ccclass, property } = _decorator;

@ccclass('Needle')
export class Needle extends Component {

    @property(Graphics)
    graphics: Graphics;

    @property(ParticleSystem2D)
    particle: ParticleSystem2D;

    @property(AudioSource)
    audio: AudioSource;

    @property(Node)
    needle: Node;

    @property(Node)
    point: Node;

    @property(CCString)
    wearType: string;

    startPos: Vec3;
    needleHandNum = -1;
    needleHandTarget: Node;
    needleHeadTarget: Node;
    needleHandPerson: Person;
    needleHeadPerson: Person;
    needleHeartPerson: Person;

    protected start(): void {
        this.Init();
    }

    update(deltaTime: number): void {
        this.graphics.clear();
        let startPoint = this.point.getPosition();
        this.graphics.moveTo(startPoint.x, startPoint.y);
        for (let i = 0; i < this.graphics.node.children.length; i++) {
            const element = this.graphics.node.children[i];
            let point = element.getPosition();
            this.graphics.lineTo(point.x, point.y);
        }
        this.graphics.stroke();
    }

    Init(): void {
        this.NeedleTouchInit();
    }

    NeedleTouchInit() {
        this.needle.on(Node.EventType.TOUCH_START, this.NeedleTouchStart, this);
        this.needle.on(Node.EventType.TOUCH_MOVE, this.NeedleTouchMove, this);
        this.needle.on(Node.EventType.TOUCH_END, this.NeedleTouchEnd, this);
        this.needle.on(Node.EventType.TOUCH_CANCEL, this.NeedleTouchEnd, this);
        let collider = this.needle.getComponent(Collider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.NeedleBeginContact, this);
        collider.on(Contact2DType.END_CONTACT, this.NeedleEndContact, this);
        this.startPos = this.needle.getWorldPosition();
    }

    NeedleTouchStart(event: EventTouch) {
        this.needle.setParent(MigaManager.Instance.touchPanel, true);
        let pos = v3(event.getUILocation().x, event.getUILocation().y);
        this.needle.setWorldPosition(pos);
        Tween.stopAllByTarget(this.needle);
        tween(this.needle)
            .to(0.5, { angle: 0 })
            .start();
    }

    NeedleTouchMove(event: EventTouch) {
        MigaManager.Instance.edgePoint = event;
        let pos = v3(event.getUILocation().x, event.getUILocation().y);
        this.needle.setWorldPosition(pos);
        if (this.wearType == "handwear" && this.needleHandPerson) {
            for (let i = 0; i < this.needleHandPerson.handContacts.length; i++) {
                const element = this.needleHandPerson.handContacts[i].getComponent(UITransform);
                if (element.getBoundingBoxToWorld().contains(event.getUILocation())) {
                    this.needleHandTarget = this.needleHandPerson.hands[i].children[2];
                    this.needleHandPerson.HandState(1, i);
                    this.needleHandNum = i;
                }
                else if (this.needleHandTarget == this.needleHandPerson.hands[i].children[2]) {
                    this.needleHandTarget = null;
                    this.needleHandPerson.HandState(0, i);
                    this.needleHandNum = -1;
                }
            }
        }
        else if (this.wearType == "headwear" && this.needleHeadPerson) {
            const element = this.needleHeadPerson.mouth.children[1].getComponent(UITransform);
            if (element.getBoundingBoxToWorld().contains(event.getUILocation())) this.needleHeadTarget = this.needleHeadPerson.mouth.children[1];
            else if (this.needleHeadTarget == this.needleHeadPerson.mouth) this.needleHeadTarget = null;
        }

        else if (this.wearType == "pacemaker") {
            if (this.needleHeartPerson) {
                let particles = this.needle.getComponentsInChildren(ParticleSystem2D);
                for (let i = 0; i < particles.length; i++) {
                    const element = particles[i];
                    if (!element.active) element.resetSystem(), this.audio.play();
                }
            }
            else {
                let particles = this.needle.getComponentsInChildren(ParticleSystem2D);
                for (let i = 0; i < particles.length; i++) {
                    const element = particles[i];
                    element.stopSystem();
                    this.audio.stop();
                }
            }
        }
    }

    NeedleTouchEnd(event: EventTouch) {
        MigaManager.Instance.edgePoint = null;
        if (this.needleHandTarget && this.needleHandPerson && this.needleHandTarget.children.length == 0) {
            this.needle.setParent(this.needleHandTarget, true);
            this.particle && this.particle.resetSystem();
        }
        else if (this.needleHeadTarget && this.needleHeadPerson && this.needleHeadTarget.children.length == 0) this.needle.setParent(this.needleHeadTarget, true);
        else if (this.wearType == "pacemaker" && this.needleHeartPerson) {
            let particles = this.needle.getComponentsInChildren(ParticleSystem2D);
            for (let i = 0; i < particles.length; i++) {
                const element = particles[i];
                element.stopSystem();
                this.audio.stop();
            }
            this.needle.setParent(this.node.children[1], true);
        }
        else {
            this.needle.setParent(this.node.children[1], true);
            this.particle && this.particle.stopSystem();
        }
        Tween.stopAllByTarget(this.needle);
        tween(this.needle)
            .to(0.5, { angle: 0, position: Vec3.ZERO }, { easing: EasingType.circOut })
            .start();
    }

    NeedleBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        let other = otherCollider.node;
        if (otherCollider.group == 1 << 1) this.needleHeartPerson = other.getComponent(Person);
        if (otherCollider.group == 1 << 4) this.needleHandPerson = other.parent.getComponent(Person);
        if (otherCollider.group == 1 << 11) this.needleHeadPerson = other.parent.getComponent(Person);
    }

    NeedleEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        let other = otherCollider.node;
        if (otherCollider.group == 1 << 1 && this.needleHeartPerson == other.getComponent(Person)) this.needleHeartPerson = null;
        if (otherCollider.group == 1 << 4 && this.needleHandPerson == other.parent.getComponent(Person)) this.needleHandPerson = null;
        if (otherCollider.group == 1 << 11 && this.needleHeadPerson == other.parent.getComponent(Person)) this.needleHeadPerson = null;
    }
}


